﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using System.Web.UI.HtmlControls;
using BLL.Common;

public partial class Policy_PolicyList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindPolicyInfo();
        }
    }

    private void BindPolicyInfo()
    {
        List<PaperInfo> userPaperList = new List<PaperInfo>();
        userPaperList = BLL.BPaper.getInstance().GetAllPolicyPage();

        if (userPaperList != null)
        {
            this.PolicyPages.RecordCount = userPaperList.Count;

            List<PaperInfo> temp = new List<PaperInfo>();
            temp.AddRange(userPaperList);
            int currentNum = 0;

            //分页数据处理
            foreach (PaperInfo info in temp)
            {
                currentNum++;
                if (currentNum < PolicyPages.PageSize * (PolicyPages.CurrentPageIndex - 1) + 1 || currentNum > PolicyPages.PageSize * PolicyPages.CurrentPageIndex)
                {
                    userPaperList.Remove(info);
                }
            }

            this.policyList.DataSource = userPaperList;
            this.policyList.DataBind();
        }
    }

    protected void policyList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {

            PaperInfo paperInfo = e.Item.DataItem as PaperInfo;
            HtmlAnchor userLink = e.Item.FindControl("userLink") as HtmlAnchor;
            Label lbl_CreateTime = e.Item.FindControl("lbl_CreateTime") as Label;
            Label lbl_Subject = e.Item.FindControl("lbl_Subject") as Label;
            Label lbl_BodyInfo = e.Item.FindControl("lbl_BodyInfo") as Label;
            userLink.HRef = Contexts.AdminPath + "/Policy/PolicyAdd.aspx?" + Contexts.UserPassportId + "=" + Contexts.GetEncrypt(paperInfo.PaperId.ToString());
            lbl_CreateTime.Text = paperInfo.DateCreated.ToString("yyyy年MM月dd日 HH时mm分ss秒");
            lbl_Subject.Text = paperInfo.Subject;
            if (paperInfo.Body != null)
            {
                string bodyInfo = paperInfo.Body.Length > 500 ? paperInfo.Body.Substring(0, 500) + "..." : paperInfo.Body;
                bodyInfo = bodyInfo.Replace("\r\n", "<br>").Replace("\n", "<br>");
                lbl_BodyInfo.Text = bodyInfo;
            }
        }
    }
    protected void PolicyPages_PageChanged(object sender, EventArgs e)
    {
        BindPolicyInfo();
    }
}